<%--
  Created by IntelliJ IDEA.
  User: 52203
  Date: 2018/12/28
  Time: 8:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <base href="<%=request.getContextPath()%>/"/>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@include file="../layUI.jsp"%>
</head>
<body>
    <c:choose>
        <c:when test="${not empty pageData}">
            <table width="100%" style="text-align: center" class="layui-table">
                <tr>
                    <th>ID</th>
                    <th>标题</th>
                    <th>内容</th>
                    <th>日期</th>
                    <th colspan="2">操作</th>
                </tr>
                <tbody id="thebody">

                </tbody>
                <tr>
                    <td colspan="5">
                        <%@include file="Page.jsp"%>
                    </td>
                </tr>
            </table>
        </c:when>
        <c:otherwise>
            <h3>没有公告了</h3>
        </c:otherwise>
    </c:choose>
    <script src="/js/jquery-2.1.0.js"></script>
    <script type="text/javascript">
        var pageCurrentNo = 1;
        var pageSize = 3;
        var totalPage = 0;
        $(function () {
            searchAnn(1)
            $("#begin").click(function () {
                pageCurrentNo = 1;
                searchAnn(pageCurrentNo);
            });
            $("#prev").click(function () {
                pageCurrentNo -= 1;
                searchAnn(pageCurrentNo);
            });
            $("#next").click(function () {
                pageCurrentNo += 1;
                searchAnn(pageCurrentNo);
            });
            $("#end").click(function () {
                pageCurrentNo = $("#totalPages").html();
                searchAnn(pageCurrentNo);
            });
        });

        function searchAnn(pageNo) {
            pageCurrentNo = pageNo;
            $("#thebody").html("");
            $.getJSON("/doAn", {"action": "paging", "pageNo": pageNo}, callback);

            function callback(result) {
                for (var i = 0; i < result.lists.length; i++) {
                    $("#thebody").append(
                        "<tr><td>" + result.lists[i].id + "</td>" +
                        "<td>" + result.lists[i].aTitle + "</td>" +
                        "<td>" + result.lists[i].aText + "</td>" +
                        "<td>" + result.lists[i].aDate + "</td>" +
                        "<td><a class='layui-btn' href='doAn?action=anQueryById&id=" + result.lists[i].id + "'>修改</a></td>" +
                        "<td><a class='layui-btn' href='doAn?action=anDelById&id=" + result.lists[i].id + "'>删除</a></td></tr>"
                    )
                }
                $("#pageNo").html(pageCurrentNo);
                $("#totalPages").html(result.totalPage);
                showhide(pageCurrentNo, result.totalPage);
            }
        }

        function showhide(currentPage, totalPages) {
            if (currentPage == 1) {
                $("#prev").hide();
                $("#next").show();
            }
            if (currentPage > 1 && currentPage < totalPages) {
                $("#prev").show();
                $("#next").show();
            }
            if (currentPage == totalPages) {
                $("#prev").show();
                $("#next").hide();
            }
            if (totalPages == 1) {
                $("#prev").hide();
                $("#next").hide();
            }
        }
    </script>
</body>
</html>
